/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.cellview.client;

import com.cleangwt.json.client.SimpleObject;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.Column;

/**
 * A {@link Column} with data type of {@link SimpleObject} that presents a data of data store name
 * or provides elements to edit to it.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class DataColumn<C> extends Column<SimpleObject, C> {

  /**
   * Creates a value column with the given data store name and the {@link Cell}.
   * 
   * @param name the data store name
   * @param cell the cell to render html elements
   */
  public DataColumn(String name, Cell<C> cell) {
    this(name, cell, false, false);
  }

  /**
   * Creates a value column with the given name,the {@link Cell}, the sortable and editable.
   * 
   * @param name the data store name
   * @param cell the cell to render html elements
   * @param sortable the sortable
   * @param editable the editable
   */
  public DataColumn(String name, Cell<C> cell, boolean sortable, boolean editable) {
    super(cell);
    setDataStoreName(name);
    setSortable(sortable);
    if (editable) {
      super.setFieldUpdater(new FieldUpdater<SimpleObject, C>() {

        @Override
        public void update(int index, SimpleObject object, C value) {
          object.put(getDataStoreName(), value);
        }
      });
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public C getValue(SimpleObject object) {
    return (C) object.get(getDataStoreName());
  }

  /**
   * @deprecated filed updater already set in constructor and never be modified.
   */
  @Deprecated
  @Override
  public void setFieldUpdater(FieldUpdater<SimpleObject, C> fieldUpdater) {
    throw new UnsupportedOperationException();
  }

}
